﻿/*
	PlayerHealth - Controls players hit points as well as the healthbar gui element.
*/

using UnityEngine;
using System.Collections;

public class PlayerHealth : MonoBehaviour {

	public int health = 4;
	public int maxHealth = 4;

	public GameObject healthbar;

	public Texture2D[] sprites;

	//public GameObject coinGUI;

	void Start () {
		//Instantiate(healthbar.gameObject);
		healthbar = new GameObject("HealthGUI");
		healthbar.transform.position = new Vector3(0.5f, 0.0f, 0.0f);
		healthbar.transform.localScale = new Vector3(0, 0, 1);
		healthbar.AddComponent("GUITexture");
		healthbar.guiTexture.pixelInset = new Rect(0, 0, 128, 64);

		health = maxHealth;

		healthbar.guiTexture.texture = sprites[health];
	}

	void ModifyHealth(int value){
		health += value;
		health = Mathf.Clamp(health, 0, maxHealth);
		healthbar.guiTexture.texture = sprites[health];
		//SetTexture(health);
		//Debug.Log("Health: " + health + " Max Health: " + maxHealth);
	}

	void SetTexture(int h){
		
		//Debug.Log("Health: " + health);
	}

	void OnGUI(){
		if(GUI.Button(new Rect(20, 40, 80, 20), "Increase")){
			ModifyHealth(1);
		}
		if(GUI.Button(new Rect(20, 70, 80, 20), "Decrease")){
			ModifyHealth(-1);
		}

		GUI.Label(new Rect(32, Screen.height - 32, 64, 32), "X" + PlayerController.instance.coins);
	}
}
